#ifndef __SWITCH_BLOCK_H__
#define __SWITCH_BLOCK_H__

#include "MountedElectricElementBlock.hpp"

namespace PocketSurvival
{
    // 开关
    class SwitchBlock : public MountedElectricElementBlock
    {
    public:
        static constexpr int32_t Index()
        {
            return 141;
        }
        virtual int32_t getIndex() override; 
        virtual const std::string &getBlockName() override;
        virtual int32_t getFace(int32_t value) override;

        virtual const std::vector<BoundingBox> &getCustomCollisionBoxes(int32_t value) override;

        static inline bool GetLeverState(int32_t value);
        static inline int32_t SetLeverState(int32_t value, bool state);
        static inline int32_t GetVoltageLevel(int32_t data);
        static inline int32_t SetVoltageLevel(int32_t data, int32_t voltageLevel);
        static inline int32_t CalculateIndex(int32_t value);
    };

    inline bool SwitchBlock::GetLeverState(int32_t value)
    {
        return (Terrain::ExtractData(value) & 1) != 0;
    }
    inline int32_t SwitchBlock::SetLeverState(int32_t value, bool state)
    {
        return Terrain::ReplaceData(value, state ? (Terrain::ExtractData(value) | 1) : (Terrain::ExtractData(value) & -2));
    }
    inline int32_t SwitchBlock::GetVoltageLevel(int32_t data)
    {
        return 15 - ((data >> 4) & 0xF);
    }
    inline int32_t SwitchBlock::SetVoltageLevel(int32_t data, int32_t voltageLevel)
    {
        return (data & -241) | (15 - (voltageLevel & 0xF) << 4);
    }
    inline int32_t SwitchBlock::CalculateIndex(int32_t value)
    {
        int32_t face = ((Terrain::ExtractData(value) >> 1) & 7);
        bool leverState = GetLeverState(value);
        return (face << 1) | (leverState ? 1 : 0);
    }

} // namespace PocketSurvival

#endif
